package com.chaoyi.controller;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.KoreanVideo;
import com.chaoyi.model.KoreanVideoDetail;
import com.chaoyi.service.EditorFileService;
import com.chaoyi.service.KoreanVideoService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 * @create 2018-10-下午2:00
 */
@Controller
@MultipartConfig
@RequestMapping("/korean/video")
public class KoreanVideoController {

    @Autowired
    private KoreanVideoService koreanVideoService;

    @Autowired
    private EditorFileService editorFileService;

    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mod =new ModelAndView("/korean/video/videoList");
        return mod;
    }

    @RequestMapping("videoList")
    @ResponseBody
    public JSONObject videoList(int offset,int limit,KoreanVideo koreanVideo) {
        JSONObject json = new JSONObject();

        int count =koreanVideoService.findCount(koreanVideo);

        List<KoreanVideo> list = new ArrayList<KoreanVideo>();
        if(count>0){
            list = koreanVideoService.findList(offset, limit,koreanVideo);
        }
        json.put("rows", list);
        json.put("total", count);
        return json;
    }


    /**
     *  新增页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addVideo")
    public ModelAndView addVideo(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mod = new ModelAndView("/korean/video/addVideo");
        return mod;
    }

    @ResponseBody
    @RequestMapping("addVideoSave")
    public JSONObject addVideoSave(KoreanVideo entity) {
        JSONObject json = new JSONObject();
        int number = koreanVideoService.addVideoSave(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }


    /**
     * 修改首页内容
     */
    @RequestMapping("/alertVideo")
    public ModelAndView alertVideo(HttpServletRequest request, HttpServletResponse response,int id) {
        ModelAndView mod = new ModelAndView("/korean/video/alertVideo");
        KoreanVideo entity = koreanVideoService.findVideoOne(id);
        mod.addObject("obj",entity);
        return mod;
    }

    /**
     * 保存修改
     */
    @ResponseBody
    @RequestMapping("edit")
    public JSONObject alertVideoSave(HttpServletRequest request, HttpServletResponse response, KoreanVideo entity) {
        JSONObject json = new JSONObject();
        int number = koreanVideoService.edit(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    @ResponseBody
    @RequestMapping("deleteVideo")
    public JSONObject deleteVideo(HttpServletRequest request, HttpServletResponse response, int id) {
        JSONObject json = new JSONObject();
        int number = koreanVideoService.deleteVideo(id);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    /**
     * 新增首页封面上传
     * @param response
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadClassifyImg")
    @ResponseBody
    public JSONObject uploadClassifyImg(HttpServletResponse response, @RequestParam("file_data") Part uploadFile) throws IOException{
        String imgUrl = null;
        try{
            imgUrl = koreanVideoService.uploadClassifyImg2WSCDN(uploadFile);
        }catch(Exception e){
            return UploadRes.error("文件上传失败,error:"+e.getMessage());
        }
        JSONObject result = UploadRes.success()
                .setFileUrl(imgUrl)
                .setFileType(FileType.IMAGE)
                .setDelelteUrl("deletefile")
                .putExtraParam("path", imgUrl).toJSON();
        return result;
    }


    /**
     * 新增视频上传
     * @param response
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploadClassifyVideo")
    @ResponseBody
    public JSONObject uploadClassifyVideo(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
        String fileUrl = null;
        try{
            fileUrl = koreanVideoService.uploadClassifyVideo2WSCDN(uploadFile);
        }catch(Exception e){
            return UploadRes.error("文件上传失败,error:"+e.getMessage());
        }
        JSONObject result = UploadRes.success()
                .setFileUrl(fileUrl)
                .setFileType(FileType.IMAGE)
                .setDelelteUrl("deletefile")
                .putExtraParam("path", fileUrl).toJSON();
        return result;
    }



    @RequestMapping("addVideoDetail")
    public ModelAndView addVideoDetail(int id,HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mod = new ModelAndView("/korean/detail/videoDetail");

        KoreanVideoDetail obj = koreanVideoService.selectKoreanVideoDetailById(id);
        if(obj==null){
            obj = new KoreanVideoDetail();
            obj.setKoreanVideoId(id);
        }
        mod.addObject("obj",obj);
        return mod;
    }

    @RequestMapping("videoDeatilSave")
    @ResponseBody
    public JSONObject videoDeatilSave(KoreanVideoDetail entity) {
        JSONObject json = new JSONObject();

        int number = 0;

        KoreanVideoDetail obj = koreanVideoService.selectKoreanVideoDetailById(entity.getKoreanVideoId());

        if(obj==null){
            number =koreanVideoService.saveDetail(entity);

        }else{
            number = koreanVideoService.editDetail(entity);

        }

        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> upload(@RequestParam("upfile")Part uploadFile) throws Exception {
        Map<String,Object> resultMap = editorFileService.uploadImages(uploadFile);
        return resultMap;
    }

}
